package io.renren.modules.sys.service.impl;

import io.renren.common.utils.Constant;
import io.renren.modules.sys.entity.TempEntity;
import io.renren.modules.sys.entity.TransactionAnalysisEntity;
import io.renren.modules.sys.entity.TransactionAnalysisStatisticsEntity;
import io.renren.modules.sys.service.TempService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;

import io.renren.modules.sys.dao.TransactionDao;
import io.renren.modules.sys.entity.TransactionEntity;
import io.renren.modules.sys.service.TransactionService;


@Service("transactionService")
public class TransactionServiceImpl extends ServiceImpl<TransactionDao, TransactionEntity> implements TransactionService {

    @Autowired
    private TempService tempService;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        String number = (String)params.get("number");
        String statu = (String)params.get("statu");
        String type = (String)params.get("type");
        String startime = (String)params.get("startime");
        String endtime = (String)params.get("endtime");
        IPage<TransactionEntity> page = this.page(
                new Query<TransactionEntity>().getPage(params),
                new QueryWrapper<TransactionEntity>().like(StringUtils.isNotBlank(number),"businessNum",number)
                        .eq(StringUtils.isNotBlank(statu),"statu",statu)
                        .eq(StringUtils.isNotBlank(type),"type",type)
                        .gt(StringUtils.isNotBlank(startime),"createtime",startime)
                        .lt(StringUtils.isNotBlank(endtime),"createtime",endtime)
                        .apply(params.get(Constant.SQL_FILTER) != null, (String)params.get(Constant.SQL_FILTER))
        );

        return new PageUtils(page);
    }

    @Override
    public List<TransactionAnalysisEntity> getAnalysisList(Map<String, Object> params){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Object shelves = params.get("shelvesid");
        Integer shelvesid = 0;
        if(shelves !=null &&!shelves.equals("")){
            shelvesid = Integer.parseInt(shelves.toString());
        }

        Object startimeObj = params.get("startime");

        Object endtimeObj = params.get("endtime");
        String startime = "";
        String endtime = "";
        if(startimeObj !=null && endtimeObj !=null&&!startimeObj.equals("")&&!endtimeObj.equals("")){
            startime = startimeObj.toString();
            endtime = endtimeObj.toString();
        }else{
            endtime = sdf.format(new Date());
            Date  currdate = new Date();
            Calendar ca = Calendar.getInstance();
            ca.setTime(currdate);
            ca.add(Calendar.DATE, -6);
            currdate = ca.getTime();
            startime = sdf.format(currdate);
        }
        tempService.remove(new QueryWrapper<TempEntity>());
        try {
            long e = sdf.parse(endtime).getTime()-sdf.parse(startime).getTime();
            long s = 1000*3600*24;
            int days = (int) (e/s);
            String times = startime;
            for(int i=0;i<=days;i++){
                TempEntity tempEntity = new TempEntity();
                tempEntity.setTime(times);
                tempService.save(tempEntity);
                if(times.equals(endtime)){
                    break;
                }else{
                    //给日期加上一天
                    Date  currdate = sdf.parse(times);
                    Calendar ca = Calendar.getInstance();
                    ca.setTime(currdate);
                    ca.add(Calendar.DATE, 1);
                    currdate = ca.getTime();
                    times = sdf.format(currdate);
                }
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        List<TransactionAnalysisEntity> list = baseMapper.getAnalysisList(shelvesid,startime,endtime);

        return list;
    }

    @Override
    public TransactionAnalysisStatisticsEntity getStatistics(Map<String, Object> params){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Object shelves = params.get("shelvesid");
        Integer shelvesid = 0;
        if(shelves !=null && !shelves.equals("")){
            shelvesid = Integer.parseInt(shelves.toString());
        }

        Object startimeObj = params.get("startime");

        Object endtimeObj = params.get("endtime");
        String startime = "";
        String endtime = "";
        if(startimeObj !=null && endtimeObj !=null&&!startimeObj.equals("")&&!endtimeObj.equals("")){
            startime = startimeObj.toString();
            endtime = endtimeObj.toString();
        }else{
            endtime = sdf.format(new Date());
            Date  currdate = new Date();
            Calendar ca = Calendar.getInstance();
            ca.setTime(currdate);
            ca.add(Calendar.DATE, -6);
            currdate = ca.getTime();
            startime = sdf.format(currdate);
        }
        TransactionAnalysisStatisticsEntity entity = baseMapper.getStatistics(shelvesid,startime,endtime);
        return entity;
    }

}
